package com.demo.sort;

import java.util.Arrays;

public class InsertSort {

    public static void main(String[] args) {
        int[] arr = {1,3,2,6,7,4};
        int[] a = insertionSort(arr);
        System.out.println(Arrays.toString(a));
    }

    public static int[] insertionSort(int[] arr) {
        if (arr.length == 0) {
            return arr;
        }
        int current;
        for (int i = 1; i < arr.length - 1; i++) {
            current = arr[i + 1];
            int preIndex = i;
            while (preIndex >= 0 && current < arr[preIndex]) {
                arr[preIndex + 1] = arr[preIndex];
                preIndex--;
            }
            arr[preIndex + 1] = current;
        }
        return arr;
    }
}
